package Eyjafjalla.action;

import Eyjafjalla.card.status.UnknownCloud_Eyjafjalla;
import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.util.Iterator;

public class ChooseHandChangeAction extends AbstractGameAction {
    private static final UIStrings uiStrings;
    public static final String[] TEXT;
    private AbstractPlayer p;
    public static int numClouded;
    private static final float DURATION;

    public ChooseHandChangeAction(int amount) {
        this.p = AbstractDungeon.player;
        this.setValues(null, null, amount);
        this.actionType = ActionType.CARD_MANIPULATION;
        this.duration = DURATION;
    }


    public void update() {
        if (this.duration == DURATION) {
            if (AbstractDungeon.getMonsters().areMonstersBasicallyDead()) {
                this.isDone = true;
                return;
            }
            if(AbstractDungeon.player.hand.isEmpty()){
                this.isDone = true;
                return;
            }
            if (this.amount < 0) {
                AbstractDungeon.handCardSelectScreen.open(TEXT[7], 99, true, true);
                AbstractDungeon.player.hand.applyPowers();
                this.tickDuration();
                return;
            }
            if (this.amount > p.hand.size())
                this.amount = p.hand.size();
            numClouded = this.amount;
            if (this.amount > 0) {
                AbstractDungeon.handCardSelectScreen.open(TEXT[7], this.amount, true, true);
            } else {
                this.isDone = true;
                return;
            }
            AbstractDungeon.player.hand.applyPowers();
            this.tickDuration();
            return;
        }

        if (!AbstractDungeon.handCardSelectScreen.wereCardsRetrieved) {
            Iterator var4 = AbstractDungeon.handCardSelectScreen.selectedCards.group.iterator();

            while(var4.hasNext()) {
                AbstractCard c = (AbstractCard)var4.next();
                c.unhover();
                c.untip();
                UnknownCloud_Eyjafjalla unknown = new UnknownCloud_Eyjafjalla();
                unknown.setCardPreview(c);
                unknown.current_x = c.current_x;
                unknown.current_y = c.current_y;
                unknown.drawScale = c.drawScale;
                unknown.angle = c.angle;
                unknown.flash();
                AbstractDungeon.player.hand.group.add(unknown);
            }
            AbstractDungeon.player.hand.refreshHandLayout();
            AbstractDungeon.player.hand.applyPowers();

            AbstractDungeon.handCardSelectScreen.wereCardsRetrieved = true;
        }

        this.tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
        TEXT = uiStrings.TEXT;
        DURATION = Settings.ACTION_DUR_XFAST;
    }
}
